//
//  SYKeyboard.m
//  SYKeyboard
//
//  Created by hc on 2019/3/1.
//  Copyright © 2019 hc. All rights reserved.
//

#import "SYKeyboard.h"
#import "SYNumKeyboard.h"
#import "SYLetterKeyboard.h"
#import "SYSymbolKeyboard.h"
#import "SYKeyboardHeader.h"

@interface SYKeyboard () <SYBaseKeyboardDelegate>

@property (nonatomic, strong) SYNumKeyboard *numKeyboard;
@property (nonatomic, strong) SYLetterKeyboard *letterKeyboard;
@property (nonatomic, strong) SYSymbolKeyboard *symbolKeyboard;

@property (nonatomic, assign) SYKeyboardType keyboardType;

@property (nonatomic, strong) NSMutableArray *keyboardArray;

@end

@implementation SYKeyboard

#pragma mark - SYBaseKeyboardDelegate
- (void)pressKey:(NSString *)key keyType:(SYKeyType)keyType keyboardType:(SYKeyboardType)keyboardType {
    switch (keyType) {
        case LetterSwitchKeyType:
        {
            [self switchKeyboardWithType:LetterKeyboard];
        }
             break;
        case NumberSwitchKeyType:
        {
            [self switchKeyboardWithType:NumberKeyboard];
        }
            break;
        case SymbolSwitchKeyType:
        {
            [self switchKeyboardWithType:SymbolKeyboard];
        }
            break;
        case UpLowSwitchKeyType:
            
            break;
        case DeleteKeyType:
        {
            if (_content.length > 0) _content = [_content substringToIndex:_content.length - 1];
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:keyboard:content:)]) {
                [self.delegate pressKey:key keyType:keyType keyboardType:keyboardType keyboard:self content:_content];
            }
        }
            break;
        case DoneKeyType:
        {
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:keyboard:content:)]) {
                [self.delegate pressKey:key keyType:keyType keyboardType:keyboardType keyboard:self content:_content];
            }
        }
            break;
        
            
        default:
        {
            _content = [_content stringByAppendingString:key];
            if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:keyboard:content:)]) {
                [self.delegate pressKey:key keyType:keyType keyboardType:keyboardType keyboard:self content:_content];
            }
        }
            break;
    }
}

- (instancetype)initWithFrame:(CGRect)frame {
    NSInteger height = SYKeyboardHeight + SYKeyboardToolHeight;
    if (SYKeyboardIS_iPhoneX) {
        height += 49;
    }
    if (self = [super initWithFrame:CGRectMake(0, 0, SYKeyboardScreenWidth, height)]) {
        self.backgroundColor = [UIColor whiteColor];
        _content = @"";
        _isChange = NO;
        _isNumKeyboard = NO;
        
        [self setupSubviews];
        
        self.keyboardArray = [[NSMutableArray alloc] initWithObjects:_numKeyboard, _letterKeyboard,_symbolKeyboard, nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
    }
    return self;
}

- (void)setupSubviews {
    UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SYKeyboardScreenWidth, SYKeyboardToolHeight)];
    [self addSubview:toolView];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(SYKeyboardScreenWidth - 80, 0, 80, SYKeyboardToolHeight)];
    button.tag = 12;
    [button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button setTitle:@"完成" forState:UIControlStateNormal];
    
    [toolView addSubview:button];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, SYKeyboardToolHeight - 1, SYKeyboardScreenWidth, 0.5)];
    lineView.backgroundColor = [UIColor grayColor];
    [self addSubview:lineView];
    
    UIView *keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, SYKeyboardToolHeight, SYKeyboardScreenWidth, SYKeyboardHeight)];
    [self addSubview:keyboardView];
    
    [keyboardView addSubview:self.numKeyboard];
    [keyboardView addSubview:self.letterKeyboard];
    [keyboardView addSubview:self.symbolKeyboard];
}

- (void)buttonTap:(UIButton *)btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(pressKey:keyType:keyboardType:keyboard:content:)]) {
        [self.delegate pressKey:@"完成" keyType:DoneKeyType keyboardType:self.keyboardType keyboard:self content:_content];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setIsNumKeyboard:(BOOL)isNumKeyboard {
    _isNumKeyboard = isNumKeyboard;
    
    self.numKeyboard.isNumKeyboard = isNumKeyboard;
    [self switchKeyboardWithType:NumberKeyboard];
}

- (void)keyboardWillShow:(NSNotification *)sender {
    _content = @"";
    if (self.isChange) {
        [self.numKeyboard changeNum];
    }else {
        [self.numKeyboard sortNum];
    }
}

/**
 快速创建键盘
 
 @param type 键盘类型
 @param delegate 代理对象
 @return 键盘对象
 */
+(SYKeyboard *)creatWithKeyboardType:(SYKeyboardType)type delegateTarget:(id<SYKeyboardDelegate>)delegate {
    SYKeyboard *keyboard = [SYKeyboard new];
    keyboard.delegate = delegate;
    [keyboard switchKeyboardWithType:type];
    return keyboard;
}


#pragma mark - 切换键盘
- (void)switchKeyboardWithType:(SYKeyboardType)keyboardType{    
    for (int i = 0; i < self.keyboardArray.count; i++) {
        SYBaseKeyboard * keyboard = self.keyboardArray[i];
        if (i == keyboardType) {
            keyboard.hidden = NO;
            self.keyboardType = keyboardType;
        }else{
            keyboard.hidden = YES;
        }
    }
}

#pragma mark - 懒加载
- (SYNumKeyboard *)numKeyboard {
    if (!_numKeyboard) {
        _numKeyboard = [SYNumKeyboard creatNumberKeyboardWithDelegateTarget:self];
        _numKeyboard.hidden = YES;
    }
    return _numKeyboard;
}
- (SYLetterKeyboard *)letterKeyboard {
    if (!_letterKeyboard) {
        _letterKeyboard = [SYLetterKeyboard creatLetterKeyboardWithDelegateTarget:self];
        _letterKeyboard.hidden = YES;
    }
    return _letterKeyboard;
}
- (SYSymbolKeyboard *)symbolKeyboard {
    if (!_symbolKeyboard) {
        _symbolKeyboard = [SYSymbolKeyboard creatLetterKeyboardWithDelegateTarget:self];
        _symbolKeyboard.hidden = YES;
    }
    return _symbolKeyboard;
}

@end
